package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.R;
import com.dto.SetmealDto;
import com.entity.Category;
import com.entity.Setmeal;
import com.service.CategoryService;
import com.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
      return R.success("新增套餐成功");
    }
    @GetMapping("/page")
    public R<Page> get(int page,int pageSize,String name){
        Page<Setmeal> pageInfo=new Page(page,pageSize);//创建分页构造器
        Page<SetmealDto> newPageInfo=new Page<>();//创建一个含有分类名称的分页构造器
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.like(name!=null,Setmeal::getName,name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,lqw);//pageinfo以及封装好了数据，但是分类名称还只是id不是name
        List<SetmealDto> list=new ArrayList<>();
        BeanUtils.copyProperties(pageInfo,newPageInfo,"records");//拷贝除了套餐集合的其他数据
        List<Setmeal> records = pageInfo.getRecords();//得到原本的套餐集合
        for (Setmeal s:records){//遍历集合
            SetmealDto setmealDto=new SetmealDto();//创建一个含有分类名称的对象
            BeanUtils.copyProperties(s,setmealDto);
            Long categoryId = s.getCategoryId();//获取分类的id
            Category category = categoryService.getById(categoryId);//通过id查到分类名称
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);//添加上分类名称
            list.add(setmealDto);//新套餐集合拷贝旧套餐集合，并且加上了分类名称
        }
        newPageInfo.setRecords(list);//重新设置套餐集合，用新的套餐集合
        return R.success(newPageInfo);
    }
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getWithDish(id);
        return R.success(setmealDto);
    }
    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> set(@PathVariable Integer status,@RequestParam List<Long> ids){
        setmealService.setStatus(status,ids);
        return R.success("修改成功");
    }
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> setmealList(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper();
        lqw.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(Setmeal::getStatus,1);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }
}
